package com.blockmeta.bbs.businesslibrary.baselist;

import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.HashMap;

import io.reactivex.Flowable;

public abstract class BaseModel<T extends BaseBean>{

    protected HashMap<String, String> mParams = new HashMap<>();

    public void PostParams(RxAppCompatActivity context){
        ServiceParams(getParams())
                .compose(context.<T>bindToLifecycle())
                .compose(RxUtil.<T>rxSchedulerHelper())
                .subscribe(new CommonSubscriber<T>(context) {

                    @Override
                    public void onNext(T t) {
                        SuccessOperation(t);

                    }

                    @Override
                    public void onError(Throwable t) {
                        FailedOperation(t);

                    }

                    @Override
                    public void onComplete() {
                        Completed();
                    }
                });

    }

    public void Completed(){

    }

    abstract void FailedOperation(Throwable e);

    abstract void SuccessOperation(BaseBean o);

    abstract HashMap<String, String> getParams();

    abstract Flowable<T> ServiceParams(HashMap<String, String> params);

    public void clearHashMap(){
        if(mParams.size() > 0){
            mParams.clear();
        }
    }


}
